
###############################################################################
## CuteTimer
## Speed-run plugin for EventScripts Python
## Officially supported games: CS:S OB
##
## File: core/admin.py
## Author: zoomj
## Description: used to authenticate players
## Version: 0.1

import services
from cutetimer.core.interface import interface

__all__ = [
    'interface_AdminManager'
]

class AdminManager(object):
    _auth = None
    _admin_overrides = []
    
    def __init__(self):
        if services.isRegistered('auth'):
            self._auth = services.use('auth')
            self._auth.registerCapability('cute_auth', self._auth.ADMIN)
            self._admin_overrides.append(self.get_admin)

    def get_admin(self, player, *args): # todo: fire auth/auth failed event?
        if len(self._admin_overrides):
            for override in self._admin_overrides:
                if hasattr(override, '__call__'):
                    if override(player, args):
                        return True # todo: support dif return types/check args
        if self._auth:
            if self._auth.isUseridAuthorized(player, 'cute_auth'):
                return True
            return False
        return False
            
    def add_admin_override(self, func, *args):
        if hasattr(func, '__call__') and func not in self._admin_overrides:
            self._admin_overrides.append(func)
    
    def remove_admin_override(self, func):
        if func in self._admin_overrides:
            self._admin_overrides.remove(func)
            
interface_AdminManager = AdminManager()
exposed = interface.iface_create('AdminManager', interface_AdminManager)
if not exposed:
    raise NameError('interface \'AdminManager\' not defined')
